#ifndef __QBOT_PARSER_H
#define __QBOT_PARSER_H

#include "qbot_master.h"
#include "../json/json.h"

#define MAX_MESSAGE 16

class QbotWebParser{

private:
    QbotMaster* robot;
    boost::function1<void, Json::Value&> msg_handler[MAX_MESSAGE];
    Json::Value ret_code;
    Json::Value robot_state;
    Json::StreamWriterBuilder json_writer;
    boost::mutex port_mutex;
    boost::posix_time::ptime init_time;
    int32_t running_time;
    uint32_t client_timeout_cnt = 0;
    boost::shared_ptr<boost::asio::io_service> ios_;
    boost::shared_ptr<boost::asio::deadline_timer> msg_timer_;
    void set_retcode(int msg_id, int code);
    void msg_timer_handler();

public:
    QbotWebParser(QbotMaster* bot);
    bool parse(Json::Value &msg);
    void set_velocity(Json::Value &msg);
    void set_power(Json::Value &msg);
    void get_robotState();
    const std::string get_retcode(){
        static std::string ret;
        ret = Json::writeString(json_writer, this->ret_code);
        return ret;
    }
    bool client_timeout(){
        return client_timeout_cnt > 20;
    }
    void reset_timeout(){
        client_timeout_cnt = 0;
    }

};

#endif
